//登录注册切换
function login_reset() {
    let reset = document.querySelector(".change_Button")
    reset.addEventListener("click", function (e) {
        let taget = e.target
        if (taget.innerHTML == "注册") {
            let buttons = document.querySelectorAll(".change_Button button")
            buttons[0].classList.remove("button_font")
            buttons[0].nextElementSibling.classList.remove("button_color")
            buttons[1].classList.add("button_font")
            buttons[1].nextElementSibling.classList.add("button_color")
            let form = document.querySelector(".form_main")
            form.style.marginLeft = "-400px";
        }
        if (taget.innerHTML == "登录") {
            let form = document.querySelector(".form_main")
            let buttons = document.querySelectorAll(".change_Button button")
            buttons[1].classList.remove("button_font")
            buttons[1].nextElementSibling.classList.remove("button_color")
            buttons[0].classList.add("button_font")
            buttons[0].nextElementSibling.classList.add("button_color")
            form.style.marginLeft = "0px";
        }
    })
}
//登录页面input焦点事件
function form_input() {
    let labels = $(".form_main label")
    let inputs = $(".form_main input")
    $(labels).click(function (e) {
        $(this).addClass("font_zise12 decide_label").next().addClass("decide_input").parent().addClass("decide_div")
    })
    $(inputs).blur(function () {
        if ($(this).val() == "") {
            console.log("1");
            $(this).removeClass("decide_input").prev().removeClass("font_zise12 decide_label").parent().removeClass("decide_div")
        } else
            $(this).parent().removeClass("decide_div")
    })
}
//注册页面input焦点事件
function form_reset() {
    let labels = $(".form_reset label")
    let inputs = $(".form_reset input")
    $(labels).click(function (e) {
        console.log(this);
        $(this).addClass("font_zise12 decide_label").next().addClass("decide_input").parent().addClass("decide_div")
    })
    $(inputs).blur(function () {
        if ($(this).val() == "") {
            console.log("1");
            $(this).removeClass("decide_input").prev().removeClass("font_zise12 decide_label").parent().removeClass("decide_div")
        } else
            $(this).parent().removeClass("decide_div")
    })
}
//登录注册密码框显示
function button_paassword() {
    let buttons = document.querySelectorAll("form .iconfont")
    $(buttons).click(function () {
        $(this).toggleClass("icon-biyan icon-zhengyan").prev().attr("type", function (i, str) {
            if (str == "text")
                return "password"
            else
                return "text"
        })
    })
}

function event_test(input, reg, str, event = "input") {
    $(input).on(event, function (e) {
        let $this = $(this)
        if (reg.test($this.val())) {
            $this.parent().next().text("");
        } else {
            $this.parent().next().text(str);
        }
    })
}
function form_test(input,reg){
    return reg.test($(input).val()) 
}

function form_event() {
     (function(){
        let inputs = $(`.form_main  input`)
        event_test(inputs[0], /[a-z,A-Z,0-9]{4,}/, "账号必须在4位及以上", "input")
        event_test(inputs[0], /[a-z,A-Z,0-9]{4,}/, "账号必须在4位及以上", "focus")
        event_test(inputs[1], /[a-z,A-Z,0-9]{8,}/, "密码必须在8位及以上", "input")
        event_test(inputs[1], /[a-z,A-Z,0-9]{8,}/, "密码必须在8位及以上", "focus")
     })();
     (function(){
        let inputs = $(`.form_reset  input`)
        event_test(inputs[0], /[a-z,A-Z,0-9]{4,}/, "账号必须在4位及以上", "input")
        event_test(inputs[0], /[a-z,A-Z,0-9]{4,}/, "账号必须在4位及以上", "focus")
        event_test(inputs[1], /^[a-z,A-Z][a-z,A-Z,0-9]{7,}/, "密码必须以字母开头在8位及以上", "input")
        event_test(inputs[1], /^[a-z,A-Z][a-z,A-Z,0-9]{7,}/, "密码必须以字母开头在8位及以上", "focus")
     })()
     let forms = $("form")
     $(forms[0]).on("submit",function(){
         let inputs = $(this).find("input")
         if(form_test(inputs[0],/[a-z,A-Z,0-9]{4,}/)&&form_test(inputs[1], /[a-z,A-Z,0-9]{8,}/)){
            promise({
                method:"post",
                url:"http://api.yuguoxy.com/api/member/login",
                data:{
                    username:$(inputs[0]).val(),
                    password:$(inputs[1]).val()
                }
            }).then(data=>{
                if(data.resultCode==-1){
                    alert("账号密码错误")
                }else{
                   location.href="./index.html"
                }
            },err=>{
                console.log("err",err);
            })
         }else{
             alert("账号密码格式错误")
         }
         return false
     })
     $(forms[1]).on("submit",function(){
        let inputs = $(this).find("input")
        if(form_test(inputs[0],/[a-z,A-Z,0-9]{4,}/)&&form_test(inputs[1], /^[a-z,A-Z][a-z,A-Z,0-9]{7,}/)){
           promise({
               method:"post",
               url:"http://api.yuguoxy.com/api/member/add",
               data:{
                   username:$(inputs[0]).val(),
                   password:$(inputs[1]).val()
               }
           }).then(data=>{
               console.log(data);
               if(data.resultCode==-1){
                   alert("注册失败")
               }else{
                alert("注册成功")
               }
           },err=>{
               console.log("err",err);
           })
        }else{
            alert("账号密码格式错误")
        }
        return false
    })
}




login_reset()
form_input()
form_reset()
button_paassword()
form_event()